package com.zhang.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import com.zhang.commonutils.R;
import com.zhang.eduservice.entity.EduTeacher;
import com.zhang.eduservice.entity.vo.TeacherQuery;
import com.zhang.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2021-05-18
 */
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询讲师列表模块
     * @return
     */

    @ApiOperation("查询讲师列表模块")
    @RequestMapping(
            value = {"/listTeacher"},
            method = {RequestMethod.GET}
    )
    public R list(){
        return R.ok().data("items",eduTeacherService.list(null));
    }

    /**
     * 根据教师id删除教师
     * @param id
     * @return
     */
    @ApiOperation("根据教师id删除教师")
    @RequestMapping(
            value = {"/remove/{id}"},
            method = {RequestMethod.DELETE}
    )
    public R removeById(@PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


    /**
     * 讲师分页查询（不带条件）
     * @param current
     * @param limit
     * @return
     */
    @ApiOperation("讲师分页查询（不带条件）")
    @RequestMapping(
            value = {"/listTeacher/{current}/{limit}"},
            method = {RequestMethod.GET}
    )
    public R pageList(
            @ApiParam(name = "current",value = "当前页码",required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable Long limit){

        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);
        eduTeacherService.page(eduTeacherPage,null);
        long total = eduTeacherPage.getTotal();
        List<EduTeacher> records = eduTeacherPage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 讲师分页查询（带条件）
     * @param current
     * @param limit
     * @param teacherQuery
     * @return
     */
    @ApiOperation("讲师分页查询（带条件）")
    @RequestMapping(
            value = {"/pageTeacherCondition/{current}/{limit}"},
            method = {RequestMethod.GET}
    )
    public R pageTeacherCondition(
            @ApiParam(name = "current",value = "当前页码",required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) TeacherQuery teacherQuery){
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //创建page对象
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);

        // 多条件组合查询
        // mybatis学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //实现分页
        eduTeacherService.page(eduTeacherPage,wrapper);
        long total = eduTeacherPage.getTotal();
        List<EduTeacher> records = eduTeacherPage.getRecords();
        return R.ok().data("total",total).data("rows",records);

    }

}

